<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/3/7
 * Time: 10:40
 */
function removeXss($data){
    require_once CORE_PATH . 'HtmlPurifier/HTMLPurifier.auto.php';
    $_clean_xss_config = HTMLPurifier_Config::createDefault();
    $_clean_xss_config->set('Core.Encoding', 'UTF-8');
    $_clean_xss_config->set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]');
    $_clean_xss_config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align,');
    $_clean_xss_config->set('HTML.TargetBlank', TRUE);
    $_clean_xss_obj = new HTMLPurifier($_clean_xss_config);
    return $_clean_xss_obj->purify($data);

}

function showImage($url,$width='',$height=''){
    $ic = C('IMAGE_CONFIG');
    if($width){
        $width = "width = '$width'";
    }
    if($height){
        $height = "height = '$height'";
    }
    echo "<img $width $height src='{$ic['viewPath']}$url'/>";
}

function uploadOne($imgName,$dirName,$thumb=array()){
    $ic = C('IMAGE_CONFIG');
    $upload = new \Think\Upload(array(
        'rootPath' => $ic['rootPath'],
        'maxSize'  => $ic['maxSize'],
        'exts'     => $ic['exts'],
    ));
    $upload->savePath = $dirName .'/';

    $info = $upload->upload(array($imgName=>$_FILES[$imgName]));
    if(!$info){
        return array(
            'ok' => 0,
            'error' => $upload->getError(),
        );
    } else {
        $ret['ok'] = 1;
        $ret['images'][0] = $logoName = $info[$imgName]['savepath'].$info[$imgName]['savename'];
        if($thumb){
            $image = new \Think\Image();
            foreach($thumb as $k => $v){
                $ret['images'][$k+1] = $info[$imgName]['savepath'] . 'thumb_'.$k.'_'.$info[$imgName]['savename'];
                $image->open($ic['rootPath'].$logoName);
                $image->thumb($v[0],$v[1])->save($ic['rootPath'].$ret['images'][$k+1]);
            }
        }
        return $ret;
    }
}

function deleteImage($image = array()){
    $savePath = C('IMAGE_CONFIG');
    foreach ($image as $v){
        unlink($savePath['rootPath'].$v);
    }
}

function getGoodCat($goods_id){
    $goods_cat_model = ('goods_cat');
    $data = $goods_cat_model->where("goods_id={$goods_id}")->select();
    foreach($data as $k=>$v){

    }
}

function buildSelect($tableName,$selectName,$valueFieldName,$textFieldName,$selectedValue=''){
    $model = D($tableName);
    $data = $model->field("$valueFieldName,$textFieldName")->select();
    $select = "<select name='$selectName'><option value=''>请选择</option>";
    foreach($data as $k => $v){
        $value = $v[$valueFieldName];
        $text = $v[$textFieldName];
        if($selectedValue && $selectedValue == $value) {
            $selected = 'selected="selected"';
        } else {
            $selected = '';
        }
         $select .= '<option '.$selected.' value="'.$value.'">'.$text.'</option>';
    }
    $select .= "</select>";
    echo $select;

}

/**********为一个订单生成一个支付宝支付按钮**********/
function makeAlipayBtn($orderId,$btnName='去支付宝支付'){
    return require('./alipay/alipayapi.php');
}

/**********从当前URL中去掉某个参数之后的URL**********/
function filterUrl($param){
    //先取出当前URL地址
    $url = $_SERVER['PHP_SELF'];
    //正则去掉某个参数
    $re = "/\/$param\/[^\/]+/";
    return preg_replace($re,'',$url);
}